package com.module_live.fragment.coachstudy.workrecord;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import com.google.android.material.tabs.TabLayout;
import com.lib.ui.fragment.BaseAppFragment;
import com.module_live.R;
import com.module_live.R2;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;


public class WorkRecordMainFragment extends BaseAppFragment {

    @BindView(R2.id.tl_tabs)
    TabLayout tlTabs;
    @BindView(R2.id.vp_content)
    ViewPager vpContent;

    RecordStudentUploadFragment uploadFragment;
    RecordStudentFinishedFragment finishedFragment;
    RecordTeacherDealedFragment dealedFragment;

    List<Fragment> fgList;
    public String[] titleArr = {"上传作业", "完成作业", "批改作业"};

    @Override
    protected void onCreateView(View view, Bundle savedInstanceState) {

        initViewPager();

        initTabNormal();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_study_recordmain;
    }


    /**
     * 初始化ViewPager，方便后期与tabLayout关联
     */
    private void initViewPager() {
        if (fgList == null)
            fgList = new ArrayList<>();

        uploadFragment = new RecordStudentUploadFragment();
        finishedFragment = new RecordStudentFinishedFragment();
        dealedFragment = new RecordTeacherDealedFragment();

        fgList.add(uploadFragment);
        fgList.add(finishedFragment);
        fgList.add(dealedFragment);

        ViewPagerAdapter pagerAdapter = new ViewPagerAdapter(getChildFragmentManager(), fgList);
        vpContent.setAdapter(pagerAdapter);
        pagerAdapter.notifyDataSetChanged();
    }


    /**
     * 初始化顶部标签
     */
    private void initTabNormal() {
        tlTabs.setupWithViewPager(vpContent);
        tlTabs.setSelectedTabIndicatorHeight(0);
        tlTabs.setSelectedTabIndicatorColor(Color.TRANSPARENT);
        tlTabs.setFocusableInTouchMode(false);
        tlTabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //添加选中Tab的逻辑

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //添加未选中Tab的逻辑
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                //再次选中tab的逻辑
            }
        });

    }

    /**
     * ViewPager的适配器。
     */
    class ViewPagerAdapter extends FragmentPagerAdapter {

        List<Fragment> fragmentList;

        public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titleArr[position];
        }
    }

}
